package org.framework.edge.builder.infrastructure.persistence;

import org.framework.edge.builder.infrastructure.entity.DockerBuilderDO;
import org.framework.edge.builder.infrastructure.converter.DockerBuilderConverter;
import org.framework.edge.builder.infrastructure.mapper.DockerBuilderMapper;
import org.framework.edge.builder.domain.model.docker.builder.DockerBuilderRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.framework.edge.builder.domain.model.docker.builder.DockerBuilder;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe docker 打包
 *
 * @author Jia wei Wu
 * @date 2025/07/07 08:10 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class DockerBuilderRepositoryImpl implements DockerBuilderRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增docker 打包
     *
     * @param dockerBuilder 新增docker 打包
     * @return {@link Result<DockerBuilder>} docker 打包新增后领域对象
     * @author Jia wei Wu
     * @date 2025/07/07 08:10 晚上
     **/

    @Override
    public Result<DockerBuilder> story(DockerBuilder dockerBuilder) {
        DockerBuilderDO dockerBuilderDO = DockerBuilderConverter.INSTANCE.fromDockerBuilder(dockerBuilder);
        lazyLambdaStream.upsert(dockerBuilderDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增docker 打包
     *
     * @param dockerBuilderList 批量新增docker 打包
     * @return {@link Result<List<DockerBuilder>>} docker 打包新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/07/07 08:10 晚上
     **/

    @Override
    public Result<List<DockerBuilder>> batchStory(List<DockerBuilder> dockerBuilderList) {
        List<DockerBuilderDO> dockerBuilderDOList = dockerBuilderList.stream().map(DockerBuilderConverter.INSTANCE::fromDockerBuilder).collect(Collectors.toList());
        lazyLambdaStream.upsert(dockerBuilderDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个docker 打包
     *
     * @param dockerBuilder 查询单个docker 打包
     * @return {@link Result<DockerBuilder>} docker 打包领域对象
     * @author Jia wei Wu
     * @date 2025/07/07 08:10 晚上
     **/

    @Override
    public Result<DockerBuilder> findOne(DockerBuilder dockerBuilder) {
        DockerBuilderDO dockerBuilderDO = DockerBuilderConverter.INSTANCE.fromDockerBuilder(dockerBuilder);
        DockerBuilder dockerBuilderOne = lazyLambdaStream.selectOne(
                LazyWrappers.<DockerBuilderDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerBuilderDO::getCode, dockerBuilderDO.getCode())
                        .eqIgnoreEmpty(DockerBuilderDO::getCreateTime, dockerBuilderDO.getCreateTime())
                        .eqIgnoreEmpty(DockerBuilderDO::getDesc, dockerBuilderDO.getDesc())
                        .eqIgnoreEmpty(DockerBuilderDO::getDockerfile, dockerBuilderDO.getDockerfile())
                        .eqIgnoreEmpty(DockerBuilderDO::getId, dockerBuilderDO.getId())
                        .eqIgnoreEmpty(DockerBuilderDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerBuilderDO::getRepositoryId, dockerBuilderDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerBuilderDO::getType, dockerBuilderDO.getType())
                        .eqIgnoreEmpty(DockerBuilderDO::getUpdateTime, dockerBuilderDO.getUpdateTime())
                , DockerBuilder.class);
        return ResultFactory.successOf(dockerBuilderOne);
    }

    /**
     * describe 查询多个docker 打包
     *
     * @param dockerBuilder 查询多个docker 打包
     * @return {@link Result<List<DockerBuilder>>} docker 打包领域对象
     * @author Jia wei Wu
     * @date 2025/07/07 08:10 晚上
     **/

    @Override
    public Result<List<DockerBuilder>> findList(DockerBuilder dockerBuilder) {
        DockerBuilderDO dockerBuilderDO = DockerBuilderConverter.INSTANCE.fromDockerBuilder(dockerBuilder);
        List<DockerBuilder> dockerBuilderList = lazyLambdaStream.selectList(
                LazyWrappers.<DockerBuilderDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerBuilderDO::getCode, dockerBuilderDO.getCode())
                        .eqIgnoreEmpty(DockerBuilderDO::getCreateTime, dockerBuilderDO.getCreateTime())
                        .eqIgnoreEmpty(DockerBuilderDO::getDesc, dockerBuilderDO.getDesc())
                        .eqIgnoreEmpty(DockerBuilderDO::getDockerfile, dockerBuilderDO.getDockerfile())
                        .eqIgnoreEmpty(DockerBuilderDO::getId, dockerBuilderDO.getId())
                        .eqIgnoreEmpty(DockerBuilderDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerBuilderDO::getRepositoryId, dockerBuilderDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerBuilderDO::getType, dockerBuilderDO.getType())
                        .eqIgnoreEmpty(DockerBuilderDO::getUpdateTime, dockerBuilderDO.getUpdateTime())
                , DockerBuilder.class);
        return ResultFactory.successOf(dockerBuilderList);
    }

    /**
     * describe 分页查询多个docker 打包
     *
     * @param size          当前页数
     * @param current       当前页
     * @param dockerBuilder 分页查询多个docker 打包
     * @return {@link Result<LazyPage<DockerBuilder>>} 分页docker 打包领域对象
     * @author Jia wei Wu
     * @date 2025/07/07 08:10 晚上
     **/

    @Override
    public Result<LazyPage<DockerBuilder>> findPage(int size, int current, DockerBuilder dockerBuilder) {
        DockerBuilderDO dockerBuilderDO = DockerBuilderConverter.INSTANCE.fromDockerBuilder(dockerBuilder);
        LazyPage<DockerBuilder> lazyPage = new LazyPage<>(current, size);
        LazyPage<DockerBuilder> dockerBuilderLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<DockerBuilderDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerBuilderDO::getCode, dockerBuilderDO.getCode())
                        .eqIgnoreEmpty(DockerBuilderDO::getCreateTime, dockerBuilderDO.getCreateTime())
                        .eqIgnoreEmpty(DockerBuilderDO::getDesc, dockerBuilderDO.getDesc())
                        .eqIgnoreEmpty(DockerBuilderDO::getDockerfile, dockerBuilderDO.getDockerfile())
                        .eqIgnoreEmpty(DockerBuilderDO::getId, dockerBuilderDO.getId())
                        .eqIgnoreEmpty(DockerBuilderDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerBuilderDO::getRepositoryId, dockerBuilderDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerBuilderDO::getType, dockerBuilderDO.getType())
                        .eqIgnoreEmpty(DockerBuilderDO::getUpdateTime, dockerBuilderDO.getUpdateTime())
                , lazyPage, DockerBuilder.class);
        return ResultFactory.successOf(dockerBuilderLazyPage);
    }

    /**
     * describe 删除docker 打包
     *
     * @param dockerBuilder 删除docker 打包
     * @return {@link Result<DockerBuilder>} docker 打包
     * @author Jia wei Wu
     * @date 2025/07/07 08:10 晚上
     **/

    @Override
    public Result<DockerBuilder> remove(DockerBuilder dockerBuilder) {
        DockerBuilderDO dockerBuilderDO = DockerBuilderConverter.INSTANCE.fromDockerBuilder(dockerBuilder);
        lazyLambdaStream.delete(
                LazyWrappers.<DockerBuilderDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerBuilderDO::getCode, dockerBuilderDO.getCode())
                        .eqIgnoreEmpty(DockerBuilderDO::getCreateTime, dockerBuilderDO.getCreateTime())
                        .eqIgnoreEmpty(DockerBuilderDO::getDesc, dockerBuilderDO.getDesc())
                        .eqIgnoreEmpty(DockerBuilderDO::getDockerfile, dockerBuilderDO.getDockerfile())
                        .eqIgnoreEmpty(DockerBuilderDO::getId, dockerBuilderDO.getId())
                        .eqIgnoreEmpty(DockerBuilderDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerBuilderDO::getRepositoryId, dockerBuilderDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerBuilderDO::getType, dockerBuilderDO.getType())
                        .eqIgnoreEmpty(DockerBuilderDO::getUpdateTime, dockerBuilderDO.getUpdateTime())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在docker 打包
     *
     * @param dockerBuilder docker 打包领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/07/07 08:10 晚上
     **/

    @Override
    public Result<Boolean> exists(DockerBuilder dockerBuilder) {
        DockerBuilderDO dockerBuilderDO = DockerBuilderConverter.INSTANCE.fromDockerBuilder(dockerBuilder);
        Boolean exists = lazyLambdaStream.exists(
                LazyWrappers.<DockerBuilderDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerBuilderDO::getCode, dockerBuilderDO.getCode())
                        .eqIgnoreEmpty(DockerBuilderDO::getCreateTime, dockerBuilderDO.getCreateTime())
                        .eqIgnoreEmpty(DockerBuilderDO::getDesc, dockerBuilderDO.getDesc())
                        .eqIgnoreEmpty(DockerBuilderDO::getDockerfile, dockerBuilderDO.getDockerfile())
                        .eqIgnoreEmpty(DockerBuilderDO::getId, dockerBuilderDO.getId())
                        .eqIgnoreEmpty(DockerBuilderDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerBuilderDO::getRepositoryId, dockerBuilderDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerBuilderDO::getType, dockerBuilderDO.getType())
                        .eqIgnoreEmpty(DockerBuilderDO::getUpdateTime, dockerBuilderDO.getUpdateTime())
        );
        return ResultFactory.successOf(exists);
    }

}